Basic Program in C


BASIC INPUT/OUTPUT STATEMENT: 

In C programming, input/output operations are essential building blocks for communicating a program to the user or other system components. The foundations of this communication, which enable dynamic interaction between C programs, are the "scanf" and "printf" routines.

scanf:

  • Programs can receive data from the user during runtime thanks to the scanf function, which acts as the standard input statement.
  • Programmers can ask users for input and store the answers they supply into variables by using scanf.
  • The first argument to this method is a format string, which indicates the intended data type of the input. Subsequent arguments define the location of the input.

printf
  • As the standard output statement, printf makes it easier to display data on the console or other output streams. 
  • It allows developers to send messages, results, or any other desired output to the user or external systems by formatting and printing data to the standard output. 
  • Printf uses format specifiers in its format string, just as scanf, to decide how to display data.

FORMAT SPECIFIER:

Format specifiers are instructions that tell the compiler how to interpret and display data in the output stream. The format and placement of the data in the output are determined by these specifiers.


  • Integer - %d

  • Float - %f

  • Character - %c

  • String - %s



Here is a basic program,

#include <stdio.h>

int main() {
    
       printf("Hello, World!\n");

    return 0;
}

Explanation:

#include <stdio.h>: This line instructs the compiler to include the standard input/output library (stdio.h). This library includes functions like printf() that are used for input and output activities.

int main() { ... }: This is the program's main function. All C programs must contain a main() function, which serves as the program's entry point. The program's execution begins with the main() function.

printf(): This line uses the printf() function to display the string "Hello, World!" on the console. The newline character (\n) transfers the cursor to the next line after printing.

return 0;  : The fact that this statement appears suggests that the application ran correctly. The program ended successfully when main() returned a value of 0. 

Post a Comment

0 Comments